<%@page import="com.funtl.framework.web.map.BaseInfo"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base href="<%=basePath%>" />
<title><%=BaseInfo.PROJECT_NAME%> - 权限管理</title>
<jsp:include page="/head.jsp" />
<script type="text/javascript">
	function add() {
		$("#dlg_manager").dialog("open");
		$("#fm_manager").form("clear");
	}
	
	function edit() {
    	var row = $("#dg_list").datagrid("getSelected");
    	if (row == null) {
    		dialog.info("请选择一条记录");
    	} else {
    		$("#dlg_manager").dialog("open");
    		$("#purview_manager_id").val(row.id);
    		$("#purview_manager_name").val(row.managerName);
    	}
    }
	
	function del() {
    	var row = $("#dg_list").datagrid("getSelected");
    	if (row == null) {
    		dialog.info("请选择一条记录");
    	} else {
    		dialog.confirm("确定要删除记录吗？", function() {
    			var data = {
    				"purviewManager.id" : row.id
    			};
    			ajax.post("system/purview/group/ajax/deleteManager", data, function(data) {
    				if (data.message == null || data.message.length == 0) {
    					$("#dg_list").datagrid("reload");
    					dialog.info("删除成功");
    				} else {
    					dialog.info(data.message);
    				}
    			});
    		});
    	}
    }

	var dlgManagerBtn = [{
	    text:"保存",
	    iconCls:"icon-ok",
	    handler:function() {
	    	if ($("#purview_manager_id").val() == "") {
	    		$("#fm_manager").attr("action", "system/purview/group/ajax/insertManager");
	    	} else {
	    		$("#fm_manager").attr("action", "system/purview/group/ajax/updateManager");
	    	}
	    	
	    	form.submit("fm_manager", function(data) {
				if (data.message == null || data.message.length == 0) {
					$("#fm_manager").form("clear");
					$("#dlg_manager").dialog("close");
					$("#dg_list").datagrid("reload");
					dialog.info("保存成功");
				} else {
					dialog.info(data.message);
				}
			});
	    }
	},{
	    text:"取消",
	    iconCls:"icon-cancel",
	    handler:function() {
	    	$("#dlg_manager").dialog("close");
	    }
	}];
	
	//权限划分
	function purview() {
		var row = $("#dg_list").datagrid("getSelected");
		if (row == null) {
    		dialog.info("请选择一条记录");
    	} else {
    		$("#dlg_purview").dialog("open");
    		dialog.addMask("dlg_purview");
    		$("#purview_group_manager_id").val(row.id);
    		
    		var data = {
    			"purviewManager.id": row.id
    		};
    		ajax.post("system/purview/group/ajax/query", data, function(data) {
    			//清空checkbox
    			$("input[name='purviewMenus']").removeAttr("checked");
    			dialog.removeMask("dlg_purview");
    			
    			if (data.message == null || data.message.length == 0) {
    				//比对权限
    				if (data.menus != null && data.menus.length > 0) {
    					var purviewMenus = $("input[name='purviewMenus']");
    					for (var i = 0 ; i < data.menus.length ; i++) {
	    					for (var x = 0 ; x < purviewMenus.length; x++) {
	    						if (data.menus[i] == $(purviewMenus[x]).val()) {
	    							$(purviewMenus[x]).attr("checked", "checked");
	    							break;
	    						}
	    					}
	    				}
    				}
    			} else {
    				dialog.info(data.message);
    			}
    		});
    	}
	}
	
	var dlgPurviewBtn = [{  
        text:"保存",
        iconCls:'icon-ok',
        handler:function() {
        	form.submit("fm_purview", function(data) {
				if (data.message == null || data.message.length == 0) {
					dialog.info("保存成功");
					$("#dlg_purview").dialog("close");
				} else {
					dialog.info(data.message);
				}
			});
        }
    },{
        text:"取消",
        iconCls:'icon-cancel',
        handler:function() {
        	$("#dlg_purview").dialog("close");
        }
    }];
</script>
</head>

<body>
	<table id="dg_list" class="easyui-datagrid" data-options="rownumbers:true,singleSelect:true,url:'system/purview/group/ajax/queryManager',toolbar:'#dg_list_toolbar'">
		<thead>
  			<tr>
  				<th data-options="field:'managerName'">管理组名称</th>
  				<th data-options="field:'createName'">创建人</th>
  				<th data-options="field:'createDate', formatter:formatter.formatDateTime">创建日期</th>
  				<th data-options="field:'updateName'">修改人</th>
  				<th data-options="field:'updateDate', formatter:formatter.formatDateTime">修改日期</th>
  			</tr>
  		</thead>
	</table>
	<div id="dg_list_toolbar" style="padding:5px;height:auto">
   		<div style="margin-bottom:5px">
   			<span>配置管理组：</span>
	  		<a class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="add();">新增</a>
			<a class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true" onclick="edit();">编辑</a>
			<a class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="del();">删除</a>
			|
			<a class="easyui-linkbutton" data-options="plain:true" onclick="purview();">权限划分</a>
		</div>
   	</div>
   	
   	<div id="dlg_manager" class="easyui-dialog" style="width:400px;height:auto;padding:10px" data-options="title:'信息管理',buttons:dlgManagerBtn,modal:true,closed:true">
   		<form id="fm_manager" method="post" action="">
   			<input id="purview_manager_id" type="hidden" name="purviewManager.id" />
   			<table align="center">
   				<tr>
	    			<td align="right">管理组名称：</td>
	    			<td><input id="purview_manager_name" class="easyui-validatebox" type="text" name="purviewManager.managerName" data-options="required:true"></input></td>
	    		</tr>
   			</table>
   		</form>
   	</div>
   	
   	<div id="dlg_purview" class="easyui-dialog" style="width:550px;height:auto;padding:10px" data-options="title:'权限划分',modal:true,closed:true,buttons:dlgPurviewBtn">
   		<form id="fm_purview" method="post" action="system/purview/group/ajax/insert">
   			<input id="purview_group_manager_id" type="hidden" name="purviewManager.id" />
   			<s:iterator value="#request.columns" var="c" status="state">
   				<div><s:property value="#c.columnName" /></div>
   				<div style="padding:20px;">
   					<s:iterator value="#request.menus" var="m" status="statem">
   						<s:if test="#m.purviewColumnId == #c.id">
   							<input name="purviewMenus" type="checkbox" value="<s:property value="#m.id" />" /><s:property value="#m.menuName" />
   						</s:if>
   					</s:iterator>
   				</div>
   			</s:iterator>
   		</form>
   	</div>
</body>
</html>